<template>
	<view class="container">

		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 背景色区域 -->
			<view class="titleNview-background"></view>
			<swiper class="carousel" :indicator-dots="true" :autoplay="true" :interval="3000">
				<swiper-item v-for="(item, index) in adList" :key="index">
					<image :src="item.img" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>
		<!-- 分类 -->
		<view class="cate-section" v-if="navList!=null && navList.length>0">
			<!-- <view class="cate-item" v-for="(item, index) in navList" :key="index" @click="navDetail(item)"> -->
			<view class="cate-item" v-for="(item, index) in navList" :key="index">
				<image :src="item.img">
				</image>
				<text>{{item.name}}</text>
			</view>
		</view>
		<view class="top">推荐商品</view>
		<view class="guess-section">
			<view v-for="(item, index) in goodsList" :key="index" class="guess-item" @click="navToDetailPage(item)">
				<view class="image-wrapper">
					<image :src="imgPath(item.picture)" mode="aspectFill"></image>
				</view>
				<text class="title clamp">{{item.goodName}}</text>
				<text class="price">￥{{item.price}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				adList: [{
						"img": "http://t15.baidu.com/it/u=994274841,1746893377&fm=224&app=112&f=JPEG?w=499&h=279"
					},
					{
						"img": "https://img2.baidu.com/it/u=2964666637,775670190&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692550800&t=a65704a0ffaa104bfa4506df0ace19ee"
					},
					{
						"img": "https://img0.baidu.com/it/u=111165468,2008185196&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692550800&t=e57f7e617107ddb959501ca5f1115f16"
					},
				],
				navList: [{
						"img": "https://img2.baidu.com/it/u=2964666637,775670190&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692550800&t=a65704a0ffaa104bfa4506df0ace19ee",
						"name": "折扇"
					},
					{
						"img": "https://img0.baidu.com/it/u=111165468,2008185196&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1692550800&t=e57f7e617107ddb959501ca5f1115f16",
						"name": "团扇"
					},
					{
						"img": "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.alicdn.com%2Fi2%2F2211395655837%2FO1CN01G2zkJ01wQp4iWeEdG_%21%210-item_pic.jpg&refer=http%3A%2F%2Fimg.alicdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1695042891&t=407f1562211b705e8f919f491671a9c6",
						"name": "蒲扇"
					},
					{
						"img": "http://t15.baidu.com/it/u=994274841,1746893377&fm=224&app=112&f=JPEG?w=499&h=279",
						"name": "定制"
					}
				],
				//分类列表
				goodsList: [],
			};
		},
		onLoad() {
			this.loadData();
		},
		methods: {
			imgPath: function(path) {
				if (path == '' || path == null || path == 'null') {
					return "/static/errorImage.jpg"
				} else if (path != '' && path.indexOf('http') >= 0) {
					return path;
				}
				console.log(this.siteHost + path);
				return this.siteHost + path;
			},
			/**
			 * 请求静态数据
			 */
			async loadData() {
				var _this = this;
				//获取首页内容数据
				this.sendRequest({
					url: "good/list",
					method: "GET",
					success: function(res) {
						let goodsList = res.data;
						console.log(goodsList);
						_this.goodsList = goodsList;
					}
				});
			},
			//详情页
			navToDetailPage(item) {
				let goodId = item.goodId;
				console.log("goodId" + goodId);
				uni.navigateTo({
					url: `/pages/index/detail?goodId=${goodId}`
				})
			}
		},
	}
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}

	.top {
		background-color: #ffffff;
		padding: 4%;
		font-weight: 800;
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;

		.titleNview-placing {
			height: var(--status-bar-height);
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 426upx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	/* 分类 */
	.cate-section {
		display: flex;
		justify-content: space-around;
		align-items: center;
		flex-wrap: wrap;
		padding: 30upx 22upx;
		background: #fff;

		.cate-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
		}

		/* 原图标颜色太深,不想改图了,所以加了透明度 */
		image {
			width: 88upx;
			height: 88upx;
			margin-bottom: 14upx;
			border-radius: 50%;
			opacity: .7;
			box-shadow: 4upx 4upx 20upx rgba(250, 67, 106, 0.3);
		}
	}

	.guess-section {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;

		.guess-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
		}
	}
</style>
